package com.incar.chuxing.utils.Dialogs;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.incar.chuxing.R;

/**
 * Created by user1 on 2017/6/29.
 */

public class LoadingDialog extends ProgressDialog {
    private static final int CHANGE_TITLE_WHAT = 1;
    private static final int CHNAGE_TITLE_DELAYMILLIS = 300;
    private static final int MAX_SUFFIX_NUMBER = 3;
    private static final char SUFFIX = '.';

    private String title;
    private TextView loading_title;
    private ImageView iv_rotate;
    private AnimationDrawable drawable;

    public LoadingDialog(Context context) {
        super(context, R.style.Loading_Dialog_Dim);
    }


    public LoadingDialog(Context context, boolean isTrans) {
        super(context, isTrans ? R.style.Loading_Dialog_Transparent : R.style.Loading_Dialog_Dim);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        setContentView(R.layout.loading_dialog_layout);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(params);
        loading_title = (TextView) findViewById(R.id.loading_title);
        loading_title.setText(title);
        iv_rotate = (ImageView) findViewById(R.id.iv_rotate);
        /*initAnim();*/
        drawable = (AnimationDrawable) iv_rotate.getDrawable();
        if (drawable != null) {
            drawable.start();
        }
    }

    @Override
    public void show() {//在要用到的地方调用这个方法
        if (drawable != null) {
            drawable.start();
        }
        super.show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    @Override
    public void onStart() {
        loading_title.setText(title);
        if (drawable != null) {
            drawable.start();
        }
        super.onStart();
    }

    @Override
    protected void onStop() {
        if (drawable != null) {
            drawable.stop();
        }
        super.onStop();
    }

    public void setTitle(CharSequence title) {
        if (TextUtils.isEmpty(title)) {
            this.title = "加载中";
        } else {
            this.title = " " + title.toString();
        }
    }

}